<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\home\controller;


use app\wechat\service\FansService;
use controller\BasicAdmin;
use service\DataService;
use service\NodeService;
use service\ToolsService;
use service\CalService;
use service\WechatService;
use think\App;
use think\Db;
use think\facade\Cache;
use service\WyismsService;


/**
 * 朋友圈功能
 * @author caicai
 *
 */
class Friend extends HomeBasic
{


    /**
     * 获取朋友圈展示
     * @return string
     */
    public function index()
    {
        $user_id = Cache::get('userInfo')['id'];//用户ID
        $data = Db::name('WechatFansRelation')->where(['user_id' => $user_id])->select();
        $friend_ids = array_column($data,'friend_is');
        // 用户信息验证
        $comments = Db::name('FriendMoments')->whereIn('user_id' , $friend_ids)->limit(10)->order('ctime','desc')->select();
        $comments = $this->CommentsInfo($comments);
        return json_encode($comments);
    }



    /**
     * 获取单条评论
     * @return string
     */
    public function getComents()
    {
        $id = $this->request->post('id');
        $comments = Db::name('FriendMoments')->where(['id'=>$id])->order('ctime','desc')->select();
        $comments = $this->CommentsInfo($comments);
        return json_encode(current($comments));
    }

    /**
     * 我的收藏，
     * 我的消息
     * 我的笔记/动态
     * 我的粉丝
     */

    public function getCollect(){
        $action_temp = Db::name('FriendAction')->where(['user_id'=>$this->user_id,'type'=>2])->select();
        $c_ids = array_column($action_temp,'id');
        $comments = Db::name('FriendMoments')->whereIn('id',$c_ids)->order('ctime','desc')->select();
        $comments = $this->CommentsInfo($comments);
        return json_encode($comments);

    }

    /**
     * 我的消息
     * 包括 收藏 点赞
     */

    public function getMessage(){
        //没读的消息
        $action_temp = Db::name('FriendAction')->where(['user_id'=>$this->user_id,'status'=>0])->select();
        $c_ids = array_column($action_temp,'id');

        $comments = Db::name('FriendMoments')->whereIn('id',$c_ids)->order('ctime','desc')->select();
        $comments = $this->CommentsInfo($comments);
        return json_encode($comments);
    }

    /**
     * 获取我的笔记信息
     * @return string
     */
    public function getMyComments(){
        //没读的消息

        $comments = Db::name('FriendMoments')->where(['user_id'=>$this->user_id])->order('ctime','desc')->select();
        $comments = $this->CommentsInfo($comments);
        return json_encode($comments);
    }

    /**
     * 获取我的粉丝
     * @return string
     */
    public function getMyFans(){
        //关注我的人
        $fans = Db::name('WechatFansRelation')->where(['user_id'=>$this->user_id,'is_follow'=>1])->select();
        return json_encode($fans);
    }

    /**
     * 我关注的人
     * @return string
     */
    public function getMyFollow() {
        //我关注的人
        $fansRelation = Db::name('WechatFansRelation')->where(['friend_id'=>$this->user_id,'is_follow'=>1])->select();
        return json_encode($fansRelation);
    }

    /**
     * 1：发布动态笔记，删除
     * 2：收藏，取消收藏
     * 3:点赞，取消点赞
     * 4：评论，删除评论
     *
     */

    /**
     * 发布动态
     */
    public function CreateCommnets() {
        $post = $this->request->post();
        $arr = ['title' , 'imglist' ,'video','user_id','location'];
        foreach ($post as $k=>$value) {

        }
    }

    /**
     * 收藏，取消收藏
     * 点赞，取消点赞
     * 评论，删除评论
     */
    public function ActionComments(){
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $comment = $this->request->post('comment','');
        $this->Action($id,$type,$comment);
    }

    /**
     * 已读状态
     */
    public function UpadteComments(){
        $id_arr = explode(',',trim($this->request->post('id'),',')) ;// 格式 1,2,3,
        //修改为已读状态
        $action = Db::name('FriendAction')->whereIn('id',$id_arr)->update(['status'=>1]);
    }

    /**
     * 取消关注
     */
    public function UpdateFans($friend_id){
        //我关注的
        $arr = ['friend_id'=>$this->user_id,'is_follow'=>1,'user_id'=>$friend_id ];
        $fans = Db::name('WechatFansRelation')->where($arr)->find();
        if (empty($fans)) {//新增
            Db::name('FriendAction')->insert($arr);
        } else {
            //取消
            Db::name('FriendAction')->where($arr)->update(['is_follow'=>0]);
        }
    }

    /**********************************公共方法*************************************/
    /**********************************公共方法*************************************/
    /**********************************公共方法*************************************/
    /**
     *
     * 获取单条笔记的相关信息
     * @param array $comments 二维数组
     * @return string
     */
    private function CommentsInfo($comments) {

        $m_ids = array_column($comments,'id');//获取动态信息ID
        //获取行为
        $action = Db::name('FriendAction')->whereIn('m_id',$m_ids)->order('ctime','desc')->select();

        foreach ($comments as &$value) {
            $value['ctime'] = ToolsService::getTimeDesc($value['ctime'],1);
            !empty($value['imglist']) && $value['imglist'] = json_decode($value['imglist'],true) ;
            $value['likenum'] = 0;
            $value['collectnum'] = 0;
            $value['commentnum'] = 0;
            foreach ($action as $item) {
                if ($value['id'] == $item['m_id']) {
                    if ($item['type'] == 1) {//评论
                        $value['likenum'] += 1;
                    } elseif ($item['type'] == 2) { //收藏
                        $value['collectnum'] += 1;
                    }elseif ($item['type'] == 3) { //评论
                        $value['commentnum'] += 1;
                        $value['commentlist'][] = [
                            'nickname' => $this->getUserById($item['user_id'])['nickname'],
                            'comment' => $item['comment'],
                            'user_id' => $item['user_id'],
                            'p_nickname' => $item['parent_id']>0 ? $this->getUserById($item['parent_id'])['nickname']:"",
                            'status' => $item['status'],
                        ];

                    }

                }
            }
        }

        return $comments;
    }

    /**
     * 根据用户ID获取用户信息
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    private function getUserById($id) {
        $res = [];
        $res = Db::name('WechatFans')->where(['id' => $id])->find();
        return $res;
    }

    /**
     * 对单条动态操作
     * @param $id
     */
    private function Action($id,$type,$comment=null) {

        $arr = ['m_id'=>$id,'type'=>$type,'user_id'=>$this->user_id ];
        $action = Db::name('FriendAction')->where($arr)->find();
        if (empty($action)) {//新增
            $type==3 && $arr['comment'] = $comment;
            Db::name('FriendAction')->insert($arr);
        } else {
            //取消
        }
    }
}




